/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.DirectoryService;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class DirectoryServiceDAO {
 private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<DirectoryService> GetAll() {
        ArrayList<DirectoryService> listDirectoryService = new ArrayList<DirectoryService>();
        DirectoryService  directoryService = null;
        try {

            strCommand = "select * from tbl_directoryService";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();

            while (rs.next()) {
                directoryService=new DirectoryService();

                directoryService.setDirectoryServiceID(rs.getInt("directoryServiceID"));
                directoryService.setDirectoryID(rs.getInt("directoryID"));
                directoryService.setServiceName(rs.getString("ServiceName"));
                directoryService.setDiscription(rs.getString("Discription"));
                directoryService.setDiServiceStatus(rs.getInt("diServiceStatus"));
 
                listDirectoryService.add(directoryService);

            }
            System.out.println("SILIIII: "+listDirectoryService.size());
            return listDirectoryService;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryServiceDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listDirectoryService;
    }

    public boolean AddDirectoryService(int directoryID, String ServiceName, String Discription) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_directoryService (directoryID,ServiceName,Discription,diServiceStatus) VALUES (?,?,?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, directoryID);
            ps.setString(2, ServiceName);
            ps.setString(3, Discription);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(DirectoryServiceDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public DirectoryService GetByID(int directoryServiceID) {

        DirectoryService directoryService = new DirectoryService();
        try {

            strCommand = "select * from tbl_directoryService WHERE directoryServiceID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, directoryServiceID);
            rs = ps.executeQuery();
            while (rs.next()) {
               
                directoryService.setDirectoryServiceID(rs.getInt("directoryServiceID"));
                directoryService.setDirectoryID(rs.getInt("directoryID"));
                directoryService.setServiceName(rs.getString("ServiceName"));
                directoryService.setDiscription(rs.getString("Discription"));
                directoryService.setDiServiceStatus(rs.getInt("diServiceStatus"));

            }
            return directoryService;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryServiceDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return directoryService;
    }

    public boolean changeStatus(int directoryServiceID, int diServiceStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_directoryService SET diServiceStatus = ? WHERE directoryServiceID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, diServiceStatus);
            ps.setInt(2, directoryServiceID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editDirectoryService(DirectoryService objDirectoryService) {
        //UPDATE table DirectoryService
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_directoryService SET directoryID = ? ,ServiceName = ? ,Discription = ? ,diServiceStatus = ? WHERE  directoryServiceID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, objDirectoryService.getDirectoryID());
            ps.setString(2, objDirectoryService.getServiceName());
            ps.setString(3, objDirectoryService.getDiscription());
            ps.setInt(4, objDirectoryService.getDiServiceStatus());
            ps.setInt(5, objDirectoryService.getDirectoryServiceID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteDirectoryService(int directoryServiceID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_directoryService WHERE directoryServiceID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, directoryServiceID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

}
